package org.fastsyncer.rest.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.fastsyncer.common.entity.RestResult;
import org.fastsyncer.core.service.ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 密码修改处理
 * @ClassName: PwdController
 * @author AE86
 * @date: 2017年7月7日 上午10:03:33
 */
@Controller
@RequestMapping(value = "/pwd")
public class PwdController {

	private static final Logger logger = LoggerFactory.getLogger(PwdController.class);

    @Autowired
    private ConfigService config;
	
	@RequestMapping("")
	public String index(ModelMap model) {
		return "pwd/index";
	}

	/**
	 * 修改密码
	 * @Title: update 
	 * @Description: 修改个人密码
	 * @param request
	 * @param pwd 密码
	 * @param newpwd 新密码
	 * @return: RestResult
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	@ResponseBody
	public RestResult update(HttpServletRequest request, @RequestParam(value = "pwd") String pwd, @RequestParam(value = "newpwd") String newpwd) {
		try {
			if (StringUtils.isBlank(pwd)||StringUtils.isBlank(newpwd)) {
				throw new NullPointerException("密码不能为空.");
			}
			return RestResult.restSuccess(config.updateAdminPwd(pwd, newpwd));
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage(), e.getClass());
			return RestResult.restFail(e.getMessage());
		}
	}
	
}
